package com.jackrain.nea.oms.dao;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.oms.model.*;
import com.jackrain.nea.util.Tools;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;

/**
 * datav
 * @author xyc
 */
@Slf4j
@Component
public class DatavDao {
    /**
     * 插入店铺单量统计表
     * @param orderShopStatisticModel
     * */
    public int orderShopInsert(OrderShopStatisticModel orderShopStatisticModel,Timestamp timec) throws Exception {

        String sql = "insert into oms_ordershopstatistic" +
                "(allorderitemnum,allorderamount,Sellernick," +
                "PlatForm,CompanyName," +
                "CREATIONDATE,MODIFIEDDATE,ISACTIVE)" +
                "values(?,?,?,?," +
                "?,?,?,?) ";
        Timestamp time = new Timestamp(System.currentTimeMillis());

        return QueryEngine.getInstance().executeUpdate(sql, new Object[]{
                orderShopStatisticModel.getAllorderitemnum(), orderShopStatisticModel.getAllorderamount(), orderShopStatisticModel.getSellernick(), orderShopStatisticModel.getPlatForm(),
                orderShopStatisticModel.getCompanyName(), timec, time, "Y"});
    }

    /**
     * 记录店铺单量统
     * */
    public void recOrderShopStatic(JSONObject objectdetail, Timestamp time){
        try {
            OrderShopStatisticModel orderShopStatisticModel = new OrderShopStatisticModel();
            orderShopStatisticModel.setAllorderitemnum(objectdetail.getInteger("ALLORDERITEMNUM") == null? 0 : objectdetail.getInteger("ALLORDERITEMNUM"));
            orderShopStatisticModel.setAllorderamount(objectdetail.getBigDecimal("ALLORDERAMOUNT") == null? new BigDecimal(0) : objectdetail.getBigDecimal("ALLORDERAMOUNT"));
            orderShopStatisticModel.setSellernick(objectdetail.getString("SELLERNICK"));
            orderShopStatisticModel.setPlatForm(objectdetail.getInteger("PLATFORM"));
            orderShopStatisticModel.setCompanyName(objectdetail.getString("COMPANYNAME"));
            orderShopInsert(orderShopStatisticModel, time);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 插入款号成交数量信息统计表
     * @param productNumStaticModel
     * */
    public int productNumInsert(ProductNumStaticModel productNumStaticModel, Timestamp timec) throws Exception {

        String sql = "insert into oms_productnumstatic" +
                "(Productcode,ProductName,TotalNum," +
                "CompanyName," +
                "CREATIONDATE,MODIFIEDDATE,ISACTIVE)" +
                "values(?,?,?,?," +
                "?,?,?) ";
        Timestamp time = new Timestamp(System.currentTimeMillis());

        return QueryEngine.getInstance().executeUpdate(sql, new Object[]{
                productNumStaticModel.getProductcode(), productNumStaticModel.getProductName(), productNumStaticModel.getTotalNum(),
                productNumStaticModel.getCompanyName(), timec, time, "Y"});
    }

    /**
     * 记录款号成交数量信息
     * */
    public void recProductNumStatic(JSONObject objectdetail, Timestamp time){
        try {
            ProductNumStaticModel productNumStaticModel = new ProductNumStaticModel();
            productNumStaticModel.setProductcode(objectdetail.getString("PRODUCTCODE"));
            productNumStaticModel.setProductName(objectdetail.getString("PRODUCTNAME"));
            productNumStaticModel.setTotalNum(objectdetail.getInteger("TOTALNUM") == null? 0 : objectdetail.getInteger("TOTALNUM"));
            productNumStaticModel.setCompanyName(objectdetail.getString("COMPANYNAME"));
            productNumInsert(productNumStaticModel, time);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 插入款号成交金额信息统计表
     * @param productPriceStaticModel
     * */
    public int productPriceInsert(ProductPriceStaticModel productPriceStaticModel, Timestamp timec) throws Exception {

        String sql = "insert into oms_productpricestatic" +
                "(Productcode,ProductName,TotalPrice," +
                "CompanyName," +
                "CREATIONDATE,MODIFIEDDATE,ISACTIVE)" +
                "values(?,?,?,?," +
                "?,?,?) ";
        Timestamp time = new Timestamp(System.currentTimeMillis());

        return QueryEngine.getInstance().executeUpdate(sql, new Object[]{
                productPriceStaticModel.getProductcode(), productPriceStaticModel.getProductName(), productPriceStaticModel.getTotalPrice(),
                productPriceStaticModel.getCompanyName(), timec, time, "Y"});
    }

    /**
     * 记录款号成交金额信息
     * */
    public void recProductPriceStatic(JSONObject objectdetail, Timestamp time){
        try {
            ProductPriceStaticModel productPriceStaticModel = new ProductPriceStaticModel();
            productPriceStaticModel.setProductcode(objectdetail.getString("PRODUCTCODE"));
            productPriceStaticModel.setProductName(objectdetail.getString("PRODUCTNAME"));
            productPriceStaticModel.setTotalPrice(objectdetail.getBigDecimal("TOTALPRICE") == null? new BigDecimal(0) : objectdetail.getBigDecimal("TOTALPRICE"));
            productPriceStaticModel.setCompanyName(objectdetail.getString("COMPANYNAME"));
            productPriceInsert(productPriceStaticModel, time);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 插入会员成交金额统计表
     * @param memberPriceStaticModel
     * */
    public int memberPriceInsert(MemberPriceStaticModel memberPriceStaticModel, Timestamp timec) throws Exception {

        String sql = "insert into oms_memberpricestatic" +
                "(Buynick,TotalPrice," +
                "CompanyName," +
                "CREATIONDATE,MODIFIEDDATE,ISACTIVE)" +
                "values(?,?,?," +
                "?,?,?) ";
        Timestamp time = new Timestamp(System.currentTimeMillis());

        return QueryEngine.getInstance().executeUpdate(sql, new Object[]{
                memberPriceStaticModel.getBuynick(), memberPriceStaticModel.getTotalPrice(),
                memberPriceStaticModel.getCompanyName(), timec, time, "Y"});
    }

    /**
     * 记录会员成交金额信息
     * */
    public void recMemberPriceStatic(JSONObject objectdetail, Timestamp time){
        try {
            MemberPriceStaticModel memberPriceStaticModel = new MemberPriceStaticModel();
            memberPriceStaticModel.setBuynick(objectdetail.getString("BUYNICK"));
            memberPriceStaticModel.setTotalPrice(objectdetail.getBigDecimal("TOTALPRICE") == null? new BigDecimal(0) : objectdetail.getBigDecimal("TOTALPRICE"));
            memberPriceStaticModel.setCompanyName(objectdetail.getString("COMPANYNAME"));
            memberPriceInsert(memberPriceStaticModel, time);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 插入区域热力图，新增区域订单销售数量表
     * @param regionOrderStaticModel
     * */
    public int regionOrderInsert(RegionOrderStaticModel regionOrderStaticModel, Timestamp timec) throws Exception {

        String sql = "insert into oms_regionorderstatic" +
                "(allorderitemnum,ProvinceName,Adcode," +
                "CompanyName," +
                "CREATIONDATE,MODIFIEDDATE,ISACTIVE)" +
                "values(?,?,?,?," +
                "?,?,?) ";
        Timestamp time = new Timestamp(System.currentTimeMillis());

        return QueryEngine.getInstance().executeUpdate(sql, new Object[]{
                regionOrderStaticModel.getAllorderitemnum(), regionOrderStaticModel.getProvinceName(),
                regionOrderStaticModel.getAdcode(), regionOrderStaticModel.getCompanyName(), timec, time, "Y"});
    }

    /**
     * 记录区域热力图，新增区域订单销售数量信息
     * */
    public void recRegionOrderStatic(JSONObject objectdetail, Timestamp time){
        try {
            RegionOrderStaticModel regionOrderStaticModel = new RegionOrderStaticModel();
            regionOrderStaticModel.setAllorderitemnum(objectdetail.getInteger("ALLORDERITEMNUM") == null? 0 : objectdetail.getInteger("ALLORDERITEMNUM"));
            regionOrderStaticModel.setProvinceName(objectdetail.getString("PROVINCENAME"));
            regionOrderStaticModel.setAdcode(objectdetail.getString("ADCODE"));
            regionOrderStaticModel.setCompanyName(objectdetail.getString("COMPANYNAME"));
            regionOrderInsert(regionOrderStaticModel, time);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 线下单量数据
     * @param retailStoreModel
     * */
    public int retailStoreInsert(RetailStoreModel retailStoreModel, Timestamp timec) throws Exception {

        String sql = "insert into oms_retail_store" +
                "(totalprice,totalcount," +
                "CompanyName," +
                "CREATIONDATE,MODIFIEDDATE,ISACTIVE)" +
                "values(?,?,?," +
                "?,?,?) ";
        Timestamp time = new Timestamp(System.currentTimeMillis());

        return QueryEngine.getInstance().executeUpdate(sql, new Object[]{
                retailStoreModel.getTotalprice(), retailStoreModel.getTotalcount(),
                retailStoreModel.getCompanyName(), timec, time, "Y"});
    }


    /**
     * 线下单量数据
     * */
    public void recRetailStore(JSONObject objectdetail, Timestamp time){
        try {
            RetailStoreModel retailStoreModel = new RetailStoreModel();
            retailStoreModel.setTotalprice(objectdetail.getBigDecimal("TOTALPRICE") == null? new BigDecimal(0) : objectdetail.getBigDecimal("TOTALPRICE"));
            retailStoreModel.setTotalcount(objectdetail.getInteger("TOTALCOUNT") == null? 0 : objectdetail.getInteger("TOTALCOUNT"));
            retailStoreModel.setCompanyName(objectdetail.getString("COMPANYNAME"));
            retailStoreInsert(retailStoreModel, time);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 云仓单量数据
     * @param retailQhModel
     * */
    public int retailQhInsert(RetailQhModel retailQhModel, Timestamp timec) throws Exception {

        String sql = "insert into oms_o2oretail_store" +
                "(totalprice,totalcount," +
                "CompanyName," +
                "CREATIONDATE,MODIFIEDDATE,ISACTIVE)" +
                "values(?,?,?," +
                "?,?,?) ";
        Timestamp time = new Timestamp(System.currentTimeMillis());

        return QueryEngine.getInstance().executeUpdate(sql, new Object[]{
                retailQhModel.getTotalprice(), retailQhModel.getTotalcount(),
                retailQhModel.getCompanyName(), timec, time, "Y"});
    }


    /**
     * 云仓单量数据
     * */
    public void recRetailQh(JSONObject objectdetail, Timestamp time){
        try {
            RetailQhModel retailQhModel = new RetailQhModel();
            retailQhModel.setTotalprice(objectdetail.getBigDecimal("TOTALPRICE") == null? new BigDecimal(0) : objectdetail.getBigDecimal("TOTALPRICE"));
            retailQhModel.setTotalcount(objectdetail.getInteger("TOTALCOUNT") == null? 0 : objectdetail.getInteger("TOTALCOUNT"));
            retailQhModel.setCompanyName(objectdetail.getString("COMPANYNAME"));
            retailQhInsert(retailQhModel, time);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
